package controllers

import (
	"coinsend/models"
	"fmt"
	"strconv"

	"github.com/astaxie/beego"
)

// Operations about Users
type UserController struct {
	beego.Controller
}

//登录验证生成token
// @router /login [post]
func (c *UserController) Login() {

	var ret models.Response
	var uid int

	username := c.GetString("Username")
	password := c.GetString("Password")

	fmt.Println(string(c.Ctx.Input.RequestBody))
	fmt.Println(username, password)
	uid, ret = models.CheckUser(username, password)
	beego.Debug("登录", username, password)

	if ret.Code == 0 {
		token := models.GeneralJwtToken(uid)
		beego.Debug("登录信息", username, token)
		ret.Data = map[string]interface{}{
			"Token": token,
			"Id":    uid,
		}
		ret.Msg = "登录成功"
	}

	c.Data["json"] = ret
	c.ServeJSON()
}

//添加后台app管理人员
// @router /adduser [post]
func (c *UserController) Adduser() {

	var ret models.Response
	var uid int
	tokenString := c.Ctx.Input.Header("Authorization")
	uid, _ = models.ParseUidFromToken(tokenString)
	if uid != 1 {
		ret.Code = 1
		ret.Msg = "添加用户失败,原因:无权限,非管理员帐号"
		c.Data["json"] = ret
		c.ServeJSON()
		return
	}

	username := c.GetString("Username")
	password := c.GetString("Password")
	Remarks := c.GetString("Remarks")
	if username == "" || password == "" {
		ret.Code = 1
		ret.Msg = "添加用户失败,原因:用户名或密码为空"
		c.Data["json"] = ret
		c.ServeJSON()
		return
	}
	beego.Debug("添加用户", username, password, Remarks)
	uid, err := models.ORMAddUser(username, password, Remarks)
	if err != nil {

		ret.Code = 1
		ret.Msg = "添加用户失败,原因:" + err.Error()
		c.Data["json"] = ret
		c.ServeJSON()
		return
	}
	beego.Debug("添加用户", username, Remarks)
	ret.Code = 0
	ret.Msg = "添加用户成功,用户ID:" + strconv.Itoa(uid)
	c.Data["json"] = ret
	c.ServeJSON()
}

//获取app管理人员
// @router /listuser [get]
func (c *UserController) Listuser() {

	var ret models.Response
	var uid int
	tokenString := c.Ctx.Input.Header("Authorization")
	uid, _ = models.ParseUidFromToken(tokenString)
	if uid != 1 {
		ret.Code = 1
		ret.Msg = "请求失败,原因:无权限,非管理员帐号"
		c.Data["json"] = ret
		c.ServeJSON()
		return
	}

	//fmt.Println(string(c.Ctx.Input.RequestBody))
	data, err := models.ORMListUser()
	if err != nil {

		ret.Code = 1
		ret.Msg = "获取用户失败,原因:" + err.Error()
		c.Data["json"] = ret
		c.ServeJSON()
		return
	}
	if len(data) <= 0 {
		ret.Data = []string{}
	} else {
		ret.Data = data
	}
	ret.Code = 0
	ret.Msg = ""
	c.Data["json"] = ret
	c.ServeJSON()
}

//删除后台app管理人员
// @router /deluser [post]
func (c *UserController) Deluser() {

	var ret models.Response
	var uid int
	tokenString := c.Ctx.Input.Header("Authorization")
	uid, _ = models.ParseUidFromToken(tokenString)
	if uid != 1 {
		ret.Code = 1
		ret.Msg = "删除用户失败,原因:无权限,非管理员帐号"
		c.Data["json"] = ret
		c.ServeJSON()
		return
	}

	delUid, err4 := strconv.Atoi(c.GetString("Id"))

	if delUid <= 0 || err4 != nil {
		ret.Code = 1
		ret.Msg = "删除用户失败,原因:参数错误"
		c.Data["json"] = ret
		c.ServeJSON()
		return
	}
	//fmt.Println(string(c.Ctx.Input.RequestBody))
	beego.Debug("删除用户", "ID:", delUid)
	_, err := models.ORMDelUser(int64(delUid))
	if err != nil {

		ret.Code = 1
		ret.Msg = "删除用户失败,原因:" + err.Error()
		c.Data["json"] = ret
		c.ServeJSON()
		return
	}

	ret.Code = 0
	ret.Msg = "删除用户成功"
	c.Data["json"] = ret
	c.ServeJSON()
}

//获取后台app管理人员
// @router /getuserbyid [post]
func (c *UserController) Getuserbyid() {

	var ret models.Response
	var uid int
	tokenString := c.Ctx.Input.Header("Authorization")
	uid, _ = models.ParseUidFromToken(tokenString)
	if uid != 1 {
		ret.Code = 1
		ret.Msg = "调用接口失败,无权限"
		c.Data["json"] = ret
		c.ServeJSON()
		return
	}

	Uid, err4 := strconv.Atoi(c.GetString("Id"))

	if Uid <= 0 || err4 != nil {
		ret.Code = 1
		ret.Msg = "参数错误"
		c.Data["json"] = ret
		c.ServeJSON()
		return
	}
	//fmt.Println(string(c.Ctx.Input.RequestBody))
	user, err := models.ORMGetUserById(int(Uid))
	if err != nil {

		ret.Code = 1
		ret.Msg = "获取用户失败,原因:" + err.Error()
		c.Data["json"] = ret
		c.ServeJSON()
		return
	}
	ret.Data = user
	ret.Code = 0
	ret.Msg = ""
	c.Data["json"] = ret
	c.ServeJSON()
}

//修改后台app管理人员
// @router /updateuserbyid [post]
func (c *UserController) Updateuserbyid() {

	var ret models.Response
	var uid int
	tokenString := c.Ctx.Input.Header("Authorization")
	uid, _ = models.ParseUidFromToken(tokenString)
	if uid != 1 {
		ret.Code = 1
		ret.Msg = "无权限,非超级管理员帐号"
		c.Data["json"] = ret
		c.ServeJSON()
		return
	}

	password := c.GetString("Password")
	Remarks := c.GetString("Remarks")
	Uid, err4 := strconv.Atoi(c.GetString("Id"))
	Status, err5 := strconv.Atoi(c.GetString("Status"))
	if err4 != nil || err5 != nil {
		ret.Code = 1
		ret.Msg = "失败原因:参数错误"
		c.Data["json"] = ret
		c.ServeJSON()
		return
	}
	beego.Debug("修改用户", "UID:", Uid, "Password:", password, Remarks)
	//fmt.Println(string(c.Ctx.Input.RequestBody))
	_, err := models.ORMUpdateUser(int64(Uid), password, Remarks, Status)
	if err != nil {

		ret.Code = 1
		ret.Msg = "修改失败,原因:" + err.Error()
		c.Data["json"] = ret
		c.ServeJSON()
		return
	}
	beego.Debug("修改用户", Uid, Remarks)
	ret.Code = 0
	ret.Msg = "修改用户成功"
	c.Data["json"] = ret
	c.ServeJSON()
}

//修改密码
// @router /updatepassword [post]
func (c *UserController) Updatepassword() {

	var ret models.Response
	var uid int
	tokenString := c.Ctx.Input.Header("Authorization")
	uid, _ = models.ParseUidFromToken(tokenString)

	passwordold := c.GetString("Passwordold")
	password := c.GetString("Password")

	if passwordold == "" || password == "" {
		ret.Code = 1
		ret.Msg = "失败原因:参数错误"
		c.Data["json"] = ret
		c.ServeJSON()
		return
	}
	//fmt.Println(string(c.Ctx.Input.RequestBody))
	_, err := models.ORMUpdateUserPassword(int64(uid), passwordold, password)
	if err != nil {

		ret.Code = 1
		ret.Msg = "修改失败,原因:" + err.Error()
		c.Data["json"] = ret
		c.ServeJSON()
		return
	}
	beego.Debug("修改密码", uid, passwordold, password)
	ret.Code = 0
	ret.Msg = "修改成功"
	c.Data["json"] = ret
	c.ServeJSON()
}

//删除币种信息
// @router /delcoin [post]
func (c *UserController) Delcoin() {

	var ret models.Response
	var uid int
	tokenString := c.Ctx.Input.Header("Authorization")
	uid, _ = models.ParseUidFromToken(tokenString)
	if uid != 1 {
		ret.Code = 1
		ret.Msg = "删除失败,原因:无权限,非管理员帐号"
		c.Data["json"] = ret
		c.ServeJSON()
		return
	}

	Id, err4 := strconv.Atoi(c.GetString("Id"))

	if Id <= 0 || err4 != nil {
		ret.Code = 1
		ret.Msg = "删除失败,原因:参数错误"
		c.Data["json"] = ret
		c.ServeJSON()
		return
	}
	beego.Debug("删除币种信息", Id)
	//fmt.Println(string(c.Ctx.Input.RequestBody))
	err := models.ORMDelCoin(Id)
	if err != nil {

		ret.Code = 1
		ret.Msg = err.Error()
		c.Data["json"] = ret
		c.ServeJSON()
		return
	}

	ret.Code = 0
	ret.Msg = "删除成功"
	c.Data["json"] = ret
	c.ServeJSON()
}

//添加币种信息
// @router /addcoin [post]
func (c *UserController) Addcoin() {

	var ret models.Response
	var uid int
	tokenString := c.Ctx.Input.Header("Authorization")
	uid, _ = models.ParseUidFromToken(tokenString)
	if uid != 1 {
		ret.Code = 1
		ret.Msg = "添加失败,原因:无权限,非管理员帐号"
		c.Data["json"] = ret
		c.ServeJSON()
		return
	}

	Symbol := c.GetString("Symbol")
	Fee, err := strconv.ParseFloat(c.GetString("Fee"), 64)
	Type, err4 := strconv.Atoi(c.GetString("Type"))
	Contract := c.GetString("Contract")
	if Symbol != "ETH" {
		if Symbol == "" || Contract == "" || err != nil || err4 != nil {
			ret.Code = 1
			ret.Msg = "添加失败,原因:参数错误"
			c.Data["json"] = ret
			c.ServeJSON()
			return
		}
	} else {
		if Symbol == "" || err != nil || err4 != nil {
			ret.Code = 1
			ret.Msg = "添加失败,原因:参数错误"
			c.Data["json"] = ret
			c.ServeJSON()
			return
		}
	}

	//fmt.Println(string(c.Ctx.Input.RequestBody))
	err = models.ORMAddCoin(Symbol, Contract, Fee, Type)
	if err != nil {

		ret.Code = 1
		ret.Msg = err.Error()
		c.Data["json"] = ret
		c.ServeJSON()
		return
	}
	beego.Debug("添加币种信息", Symbol, Contract, Fee, Type)
	ret.Code = 0
	ret.Msg = "添加成功"
	c.Data["json"] = ret
	c.ServeJSON()
}

//修改币种信息
// @router /updatecoin [post]
func (c *UserController) Updatecoin() {

	var ret models.Response
	var uid int
	tokenString := c.Ctx.Input.Header("Authorization")
	uid, _ = models.ParseUidFromToken(tokenString)

	Id, err4 := strconv.Atoi(c.GetString("Id"))
	Fee, err := strconv.ParseFloat(c.GetString("Fee"), 64)
	if Id == 0 || Fee == 0 || err != nil || err4 != nil {
		ret.Code = 1
		ret.Msg = "添加失败,原因:参数错误"
		c.Data["json"] = ret
		c.ServeJSON()
		return
	}
	fmt.Println(Id, Fee)
	err = models.ORMUpdateFee(Id, Fee)
	if err != nil {

		ret.Code = 1
		ret.Msg = err.Error()
		c.Data["json"] = ret
		c.ServeJSON()
		return
	}
	beego.Debug("修改币种信息", uid, Id, Fee)
	ret.Code = 0
	ret.Msg = "修改成功"
	c.Data["json"] = ret
	c.ServeJSON()
}

//获取所有币种
// @router /listcoin [get]
func (c *UserController) Listcoin() {

	var ret models.Response

	//fmt.Println(string(c.Ctx.Input.RequestBody))
	data, err := models.ORMListCoin()
	if err != nil {

		ret.Code = 1
		ret.Msg = "获取失败,原因:" + err.Error()
		c.Data["json"] = ret
		c.ServeJSON()
		return
	}
	if len(data) <= 0 {
		ret.Data = []string{}
	} else {
		ret.Data = data
	}

	ret.Code = 0
	ret.Msg = ""
	c.Data["json"] = ret
	c.ServeJSON()
}

//添加地址
// @router /addaddress [post]
func (c *UserController) Addaddress() {

	var ret models.Response
	var uid int
	tokenString := c.Ctx.Input.Header("Authorization")
	uid, _ = models.ParseUidFromToken(tokenString)

	Symbol := c.GetString("Symbol")
	Address := c.GetString("Address")

	if Symbol == "" || Address == "" {
		ret.Code = 1
		ret.Msg = "添加失败,原因:参数错误"
		c.Data["json"] = ret
		c.ServeJSON()
		return
	}

	//fmt.Println(string(c.Ctx.Input.RequestBody))
	err := models.ORMAddAddress(Symbol, Address, uid)
	if err != nil {

		ret.Code = 1
		ret.Msg = err.Error()
		c.Data["json"] = ret
		c.ServeJSON()
		return
	}
	beego.Debug("添加币种信息", Symbol, Address, uid)
	ret.Code = 0
	ret.Msg = "添加成功"
	c.Data["json"] = ret
	c.ServeJSON()
}

//获取个人地址列表
// @router /listaddress [get]
func (c *UserController) Listaddress() {

	var ret models.Response
	var uid int
	tokenString := c.Ctx.Input.Header("Authorization")
	uid, _ = models.ParseUidFromToken(tokenString)

	//fmt.Println(string(c.Ctx.Input.RequestBody))
	data, err := models.ORMListAddress(uid)
	if err != nil {

		ret.Code = 1
		ret.Msg = err.Error()
		c.Data["json"] = ret
		c.ServeJSON()
		return
	}
	if len(data) <= 0 {
		ret.Data = []string{}
	} else {
		ret.Data = data
	}
	ret.Code = 0
	ret.Msg = ""
	c.Data["json"] = ret
	c.ServeJSON()
}

//删除地址
// @router /deladdress [post]
func (c *UserController) Deladdress() {

	var ret models.Response
	var uid int
	tokenString := c.Ctx.Input.Header("Authorization")
	uid, _ = models.ParseUidFromToken(tokenString)

	delUid, err4 := strconv.Atoi(c.GetString("Id"))

	if delUid <= 0 || err4 != nil {
		ret.Code = 1
		ret.Msg = "删除失败,原因:参数错误"
		c.Data["json"] = ret
		c.ServeJSON()
		return
	}
	//fmt.Println(string(c.Ctx.Input.RequestBody))
	err := models.ORMDelAddress(int(delUid), int(uid))
	if err != nil {

		ret.Code = 1
		ret.Msg = err.Error()
		c.Data["json"] = ret
		c.ServeJSON()
		return
	}

	ret.Code = 0
	ret.Msg = "删除成功"
	c.Data["json"] = ret
	c.ServeJSON()
}
